package com.dsiab.manageService.service.impl;

import com.dsiab.manageService.entity.CateDto;
import com.dsiab.manageService.entity.NavigationDto;
import com.dsiab.manageService.mapper.NavigationDao;
import com.dsiab.manageService.service.NavigationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class NavigationServiceImpl implements NavigationService {

  @Autowired
  NavigationDao navigationDao;

  public List<Map> getAll() {
    List<Map> cateList = navigationDao.getCate();
    for (Map cate: cateList){
      cate.put("children", navigationDao.getListByCate(cate.get("id").toString()));
    }
    return cateList;
  }

  public NavigationDto getDetail(int  id) {
    return navigationDao.getDetail(id);
  }

  public void insertOne(NavigationDto navigationDto) {
    navigationDao.insertOne(navigationDto);
  }

  public void delOne(int id) {
    navigationDao.delOne(id);
  }

  public void updateOne(NavigationDto navigationDto) {
    navigationDao.updateOne(navigationDto);
  }

  public Map<String, Object> getCate(int pageNum, int pageSize) {
    // 使用pageHelper分页
    Map<String, Object> resultMap = new HashMap<>();
    // 通过字段排序
    PageHelper.startPage(pageNum,pageSize);
    PageInfo<CateDto> PageInfo = new PageInfo<>(navigationDao.getCate());
    resultMap.put("result", PageInfo.getList());
    resultMap.put("total", PageInfo.getTotal());
    return resultMap;
  }

  public void insertCate(CateDto cateDto) {
    navigationDao.insertCate(cateDto);
  }

  public void updateCate(CateDto cateDto) {
    navigationDao.updateCate(cateDto);
  }

  public void delCate(int id) {
    navigationDao.delCate(id);
  }
}
